from ..common import *
from ..basetools import *

__all__ = [
    'startproject', # 新建项目
]

def startproject(path: str, project_name: str)->int:
    """新建项目，成功返回0，失败返回1
    
        校验名称必须只能用字母加数字 + （指定路径下创建）项目名不允许重复
    """
    if retools.PATT_CHARSNUMBER.match(project_name) and not os.path.exists(os.path.join(path, project_name)):
        """project_name"""
        os.mkdir(os.path.join(path, project_name))

        path = os.path.join(path, project_name)
        os.mkdir(os.path.join(path, project_name))

        PDir = os.path.join(path, project_name)
        new_file(os.path.join(PDir, '__init__.py'))
        new_file(os.path.join(PDir, 'urls.py'), content=get_content('urls.django', concat=['project']))
        new_file(os.path.join(PDir, 'asgi.py'), content=get_content('asgi.django', concat=['project'], replace=True, project_name=project_name))
        new_file(os.path.join(PDir, 'wsgi.py'), content=get_content('wsgi.django', concat=['project'], replace=True, project_name=project_name))
        new_file(os.path.join(PDir, 'settings.py'), content=get_content('settings.django', concat=['project'], replace=True, project_name=project_name, secret_key=generate_secret_key()))
        
        """templates"""
        # os.mkdir(os.path.join(path, 'templates'))
        # os.mkdir(os.path.join(path, 'templates', 'includes'))
        # new_file(os.path.join(path, 'templates', 'base.html'), content=get_content('baseHtml.django'))

        """static"""
        # os.mkdir(os.path.join(path, 'static'))
        # os.mkdir(os.path.join(path, 'static', 'js'))
        # os.mkdir(os.path.join(path, 'static', 'img'))
        # os.mkdir(os.path.join(path, 'static', 'css'))

        """manage.py"""
        new_file(os.path.join(path, 'manage.py'), content=get_content('manage.django', concat=['project'], replace=True, project_name=project_name))

        return 0
    else:
        return 1
